using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using System.Collections.Generic;
using UnityEngine;

namespace IQIGame.Onigao.GamePlay
{
    public class UIN_HeroInfoWarehouse : UIBaseNode
    {

        #region Template Generate,don't modify
        protected partial class UIB_UIN_HeroInfoWarehouse
        {
            #region ObjectBinding Generate 
            public IQIGame.Onigao.Framework.ExButton buttonCloseList { protected set; get; }
            public GameObject toggleFilter { protected set; get; }
            public UnityEngine.UI.ExLoopVerticalScrollRect loopVerticalScrollRect { protected set; get; }
            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("buttonCloseList", out var __tbv0);
                this.buttonCloseList = __tbv0;
                __binding.TryGetVariableValue<UnityEngine.GameObject>("toggleFilter", out var __tbv1);
                this.toggleFilter = __tbv1;
                __binding.TryGetVariableValue<UnityEngine.UI.ExLoopVerticalScrollRect>("loopVerticalScrollRect", out var __tbv2);
                this.loopVerticalScrollRect = __tbv2;
            }

            #endregion ObjectBinding Generate 
        }
        #endregion Template Generate,don't modify

        #region fields & properties
        ListScrollAdapter<UIN_HeroListFilter> _tabs = null;
        LoopScrollAdapter<UIN_CommonHeroHead> _loopCells = null;

        int _indexSelectedTab = -1;
        List<HeroData> _dataList = new List<HeroData>();
        HeroData _heroData = null;

        protected UIB_UIN_HeroInfoWarehouse ui { get; set; }
        #endregion

        protected override void BeforeInit()
        {
            ui = new UIB_UIN_HeroInfoWarehouse();
            ui.InitBinding(this.csObjBind);
        }

        protected override void OnInit()
        {
            this._tabs = this.InitListScroll<UIN_HeroListFilter>(this.ui.toggleFilter, this.OnTabChanged);
            this._tabs.InitUnitSelected(UnitSelectType.ByExToggle, this.OnTabSelected);
            this._loopCells = this.InitLoopScroll<UIN_CommonHeroHead>(this.ui.loopVerticalScrollRect, this.OnHeroChanged);
            this._loopCells.InitUnitSelected(UnitSelectType.ByExButton, this.OnHeroSelected, this.OnHeroUnSelected);
            this.ui.buttonCloseList.onClick.AddListener(this.OnClickHide);
        }

        protected override void OnShow()
        {

        }

        protected override void OnHide()
        {

        }

        protected override void OnDispose()
        {
            this._heroData = null;
        }

        void OnTabChanged(UIN_HeroListFilter item, int index)
        {
            item.Show(index);
        }

        void OnTabSelected(int index, UIN_HeroListFilter item)
        {
            this._indexSelectedTab = index;
            this.ViewList(item);
        }

        void ViewList(UIN_HeroListFilter tab = null)
        {
            if (tab == null)
                tab = this._tabs.GetTEntity(this._indexSelectedTab);
            int tabIndex = tab.tabIndex;
            this._dataList.Clear();
            HeroModule.Instance.GetHeros(tabIndex, this._dataList, this.SortListFunc);

            int cnt = this._dataList.Count;
            int _seleIndex = -1;
            for (int i = 0; i < cnt; i++)
            {
                if (this._heroData == this._dataList[i])
                {
                    _seleIndex = i;
                    break;
                }

            }
            this._loopCells.RefillCellsAndSelected(cnt, _seleIndex);
        }

        int SortListFunc(HeroData a, HeroData b)
        {
            if (a.isGeted != b.isGeted)
                return a.isGeted ? -1 : 1;
            if (a.level != b.level)
                return a.level > b.level ? -1 : 1;
            if (a.exp != b.exp)
                return a.exp > b.exp ? -1 : 1;
            if (a.star != b.star)
                return a.star > b.star ? -1 : 1;
            return a.cid < b.cid ? -1 : 1;
        }

        void OnHeroChanged(UIN_CommonHeroHead item, int index)
        {
            var data = this._dataList[index];
            item.Show(data);
        }

        void OnHeroSelected(int index, UIN_CommonHeroHead item)
        {
            item?.SetSelected(true);
            if (item == null)
                return;
            this._heroData = this._dataList[index];
            MsgDispatcher.Broadcast(MsgEventType.HeroInfoRefreshByChangeHero, this._heroData);
        }

        void OnHeroUnSelected(int index, UIN_CommonHeroHead item)
        {
            item?.SetSelected(false);
        }

        public void Show(HeroData data)
        {
            this.Show();
            this._heroData = data;
            int cnt = (int)EM_HeroConst.MaxLength;
            this._tabs.RefillCells(cnt, 0);
        }

        void OnClickHide()
        {
            this.Hide();
        }
    }
}
